package ckw.gui.wizards;

import java.util.ArrayList;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import ckw.database.VotingCard;
import ckw.database.VotingSubcard;
import ckw.database.VotingSubcardOption;
import org.eclipse.swt.widgets.Label;

public class WzdElectionPg4 extends WizardPage {
	private ArrayList<VotingCard> votingCardList;
	private Tree tree;
	private Button btnDodajKarte;
	private Button btnDodajListe;
	private Button btnDodajOpcje;
	private Button btnUsun;
	private Button btnEdytuj;

	/**
	 * Create the wizard.
	 */
	public WzdElectionPg4() {
		super("wizardPage");
		setTitle("Dodaj wybory");
		setDescription("Krok 4. Utworz karte wyborcza");

		votingCardList = new ArrayList<VotingCard>();
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		container.setLayout(new GridLayout(2, false));

		tree = new Tree(container, SWT.BORDER);
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 7));
		tree.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				TreeItem trtmSelected = tree.getSelection()[0];
				if (trtmSelected.getData().getClass() == VotingCard.class) {
					btnDodajListe.setEnabled(true);
					btnDodajOpcje.setEnabled(false);
				} else if (trtmSelected.getData().getClass() == VotingSubcard.class) {
					btnDodajListe.setEnabled(false);
					btnDodajOpcje.setEnabled(true);
				} else if (trtmSelected.getData().getClass() == VotingSubcardOption.class) {
					btnDodajListe.setEnabled(false);
					btnDodajOpcje.setEnabled(false);
				}
				btnEdytuj.setEnabled(true);
				btnUsun.setEnabled(true);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});

		btnDodajKarte = new Button(container, SWT.NONE);
		btnDodajKarte.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				WzdElectionDlgVotingCardAddEdit dlg = new WzdElectionDlgVotingCardAddEdit(
						getShell(), votingCardList, new VotingCard());
				if (dlg.open() == IDialogConstants.OK_ID) {
					UpdateTree();
				}
			}
		});
		btnDodajKarte.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false,
				false, 1, 1));
		btnDodajKarte.setText("Dodaj karte");

		btnDodajListe = new Button(container, SWT.NONE);
		btnDodajListe.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				VotingCard votingCard = (VotingCard) tree.getSelection()[0]
						.getData();
				WzdElectionDlgVotingSubcardAddEdit dlg = new WzdElectionDlgVotingSubcardAddEdit(
						getShell(), votingCard, new VotingSubcard());
				if (dlg.open() == IDialogConstants.OK_ID) {
					UpdateTree();
				}
			}
		});
		btnDodajListe.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnDodajListe.setText("Dodaj liste");

		btnDodajOpcje = new Button(container, SWT.NONE);
		btnDodajOpcje.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				VotingSubcard votingSubcard = (VotingSubcard) tree
						.getSelection()[0].getData();
				WzdElectionDlgVotingSubcardOptionAddEdit dlg = new WzdElectionDlgVotingSubcardOptionAddEdit(
						getShell(), votingSubcard, new VotingSubcardOption());
				if (dlg.open() == IDialogConstants.OK_ID) {
					UpdateTree();
				}
			}
		});
		btnDodajOpcje.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false,
				false, 1, 1));
		btnDodajOpcje.setText("Dodaj opcje");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		btnEdytuj = new Button(container, SWT.NONE);
		btnEdytuj.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TreeItem trtmSelected = tree.getSelection()[0];
				if (trtmSelected.getData().getClass() == VotingCard.class) {
					WzdElectionDlgVotingCardAddEdit dlg = new WzdElectionDlgVotingCardAddEdit(
							getShell(), votingCardList, (VotingCard) trtmSelected.getData());
					if (dlg.open() == IDialogConstants.OK_ID) {
						UpdateTree();
					}
				} else if (trtmSelected.getData().getClass() == VotingSubcard.class) {
					TreeItem trtmParent = trtmSelected.getParentItem();
					WzdElectionDlgVotingSubcardAddEdit dlg = new WzdElectionDlgVotingSubcardAddEdit(
							getShell(), (VotingCard) trtmParent.getData(), (VotingSubcard) trtmSelected.getData());
					if (dlg.open() == IDialogConstants.OK_ID) {
						UpdateTree();
					}
				} else if (trtmSelected.getData().getClass() == VotingSubcardOption.class) {
					TreeItem trtmParent = trtmSelected.getParentItem();
					WzdElectionDlgVotingSubcardOptionAddEdit dlg = new WzdElectionDlgVotingSubcardOptionAddEdit(
							getShell(), (VotingSubcard) trtmParent.getData(), (VotingSubcardOption) trtmSelected.getData());
					if (dlg.open() == IDialogConstants.OK_ID) {
						UpdateTree();
					}
				}
			}
		});
		btnEdytuj.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false,
				1, 1));
		btnEdytuj.setText("Edytuj");
		btnEdytuj.setEnabled(false);

		btnUsun = new Button(container, SWT.NONE);
		btnUsun.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TreeItem trtmSelected = tree.getSelection()[0];
				if (trtmSelected.getData().getClass() == VotingCard.class) {
					votingCardList.remove((VotingCard) trtmSelected.getData());
				} else if (trtmSelected.getData().getClass() == VotingSubcard.class) {
					VotingSubcard votingSubcard = (VotingSubcard) trtmSelected
							.getData();
					votingSubcard.removeFromVotingCard(votingSubcard
							.getVotingCard());
				} else if (trtmSelected.getData().getClass() == VotingSubcardOption.class) {
					VotingSubcardOption votingSubcardOption = (VotingSubcardOption) trtmSelected
							.getData();
					votingSubcardOption
							.removeFromVotingSubcard(votingSubcardOption
									.getVotingSubcard());
				}
				UpdateTree();
			}
		});
		btnUsun.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1,
				1));
		btnUsun.setText("Usun");
		tree.deselectAll();

		btnDodajListe.setEnabled(false);
		btnDodajOpcje.setEnabled(false);

		btnEdytuj.setEnabled(false);
		btnUsun.setEnabled(false);
	}

	private void UpdateTree() {
		tree.removeAll();
		for (VotingCard votingCard : votingCardList) {
			TreeItem trtmVotingCard = new TreeItem(tree, SWT.NONE);
			trtmVotingCard.setText(votingCard.getName());
			trtmVotingCard.setData(votingCard);
			for (VotingSubcard votingSubcard : votingCard.getVotingSubcards()) {
				TreeItem trtmVotingSubcard = new TreeItem(trtmVotingCard,
						SWT.NONE);
				trtmVotingSubcard.setText(votingSubcard.getName());
				trtmVotingSubcard.setData(votingSubcard);
				for (VotingSubcardOption votingSubcardOption : votingSubcard
						.getVotingSubcardOptions()) {
					TreeItem trtmVotingSubcardOption = new TreeItem(
							trtmVotingSubcard, SWT.NONE);
					trtmVotingSubcardOption.setText(votingSubcardOption
							.getName());
					trtmVotingSubcardOption.setData(votingSubcardOption);
				}
			}
		}
		btnDodajListe.setEnabled(false);
		btnDodajOpcje.setEnabled(false);

		btnEdytuj.setEnabled(false);
		btnUsun.setEnabled(false);
		for (TreeItem treeItem : tree.getItems()) {
			treeItem.setExpanded(true);
		}
	}

	public ArrayList<VotingCard> getElectionVotingCardList() {
		return votingCardList;
	}
}
